﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="VehicleTransactionRepositoryTest.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the VehicleTransactionRepositoryTest type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Data.Tests
{
    using System.Data.Objects;
    using System.Linq;
    using Entities;
    using Interfaces;
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Moq;

    /// <summary>
    /// Defines the <seealso cref="Hisaab.Data.Tests.VehicleTransactionRepositoryTest"/> type.
    /// </summary>
    [TestClass]
    public class VehicleTransactionRepositoryTest
    {
        #region VehicleTransaction Tests

        #region Query Tests

        /// <summary>
        /// VehicleTransactions the data manager get VehicleTransactions test with empty repository.
        /// </summary>
        [TestMethod]
        public void VehicleTransactionDataManagerQueryShouldReturnEmptyListWithEmptyRepository()
        {
            var mockObjectSet = new MockObjectSet<VehicleTransaction>();
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.VehicleTransactions).Returns(mockObjectSet);
            var dao = new VehicleTransactionRepository(mockRepository.Object);
            var result = dao.Query(new VehicleTransaction(), null, null);
            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.Count());
            mockRepository.VerifyAll();
        }

        /// <summary>
        /// VehicleTransactions the data manager get VehicleTransactions test with non empty repository.
        /// </summary>
        [TestMethod]
        public void VehicleTransactionDataManagerQueryShouldReturnNonEmptyListWithNonEmptyRepository()
        {
            var mockObjectSet = new MockObjectSet<VehicleTransaction>();
            mockObjectSet.AddObject(new VehicleTransaction { Id = 1, Milage = 4 });
            mockObjectSet.AddObject(new VehicleTransaction { Id = 2, Milage = 3 });
            mockObjectSet.AddObject(new VehicleTransaction { Id = 3, Milage = 5 });
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.VehicleTransactions).Returns(mockObjectSet);
            var dao = new VehicleTransactionRepository(mockRepository.Object);
            var result = dao.Query(new VehicleTransaction(), null, null);
            Assert.IsNotNull(result);
            Assert.AreEqual(3, result.Count());
            Assert.AreEqual(1, result.ElementAt(0).Id);
            Assert.AreEqual(2, result.ElementAt(1).Id);
            Assert.AreEqual(3, result.ElementAt(2).Id);
            mockRepository.VerifyAll();
        }

        #endregion Query Tests

        #region Filter Tests

        /// <summary>
        /// VehicleTransactions the data manager get VehicleTransactions test with empty repository.
        /// </summary>
        [TestMethod]
        public void VehicleTransactionDataManagerFilterShouldReturnEmptyListWithEmptyRepository()
        {
            var mockObjectSet = new MockObjectSet<VehicleTransaction>();
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.VehicleTransactions).Returns(mockObjectSet);
            var dao = new VehicleTransactionRepository(mockRepository.Object);
            var result = dao.Filter(x => true, null, null);
            Assert.IsNotNull(result);
            Assert.AreEqual(0, result.Count());
            mockRepository.VerifyAll();
        }

        /// <summary>
        /// VehicleTransactions the data manager get VehicleTransactions test with non empty repository.
        /// </summary>
        [TestMethod]
        public void VehicleTransactionDataManagerFilterShouldReturnNonEmptyListWithNonEmptyRepository()
        {
            var mockObjectSet = new MockObjectSet<VehicleTransaction>();
            mockObjectSet.AddObject(new VehicleTransaction { Id = 1, Milage = 4 });
            mockObjectSet.AddObject(new VehicleTransaction { Id = 2, Milage = 3 });
            mockObjectSet.AddObject(new VehicleTransaction { Id = 3, Milage = 5 });
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.VehicleTransactions).Returns(mockObjectSet);
            var dao = new VehicleTransactionRepository(mockRepository.Object);
            var result = dao.Filter(x => true, null, null);
            Assert.IsNotNull(result);
            Assert.AreEqual(3, result.Count());
            Assert.AreEqual(1, result.ElementAt(0).Id);
            Assert.AreEqual(2, result.ElementAt(1).Id);
            Assert.AreEqual(3, result.ElementAt(2).Id);
            mockRepository.VerifyAll();
        }

        #endregion Filter Tests

        #region GetById Tests

        /// <summary>
        /// Get by id test with empty repository
        /// </summary>
        [TestMethod]
        public void VehicleTransactionDataManagerFindByIdShouldReturnNullWithEmptyRepository()
        {
            var mockObjectSet = new MockObjectSet<VehicleTransaction>();
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.VehicleTransactions).Returns(mockObjectSet);
            var dao = new VehicleTransactionRepository(mockRepository.Object);
            var result = dao.FindById(1);
            Assert.IsNull(result);
            mockRepository.VerifyAll();
        }

        /// <summary>
        /// Get by id test with repository with records
        /// </summary>
        [TestMethod]
        public void VehicleTransactionDataManagerFindByIdShouldReturnMatchingVehicleTransaction()
        {
            var mockObjectSet = new MockObjectSet<VehicleTransaction>();
            mockObjectSet.AddObject(new VehicleTransaction { Id = 1, Milage = 4 });
            mockObjectSet.AddObject(new VehicleTransaction { Id = 2, Milage = 3 });
            mockObjectSet.AddObject(new VehicleTransaction { Id = 3, Milage = 5 });
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.VehicleTransactions).Returns(mockObjectSet);
            var dao = new VehicleTransactionRepository(mockRepository.Object);
            var result = dao.FindById(2);
            Assert.IsNotNull(result);
            Assert.AreEqual(2, result.Id);
            mockRepository.VerifyAll();
        }

        /// <summary>
        /// VehicleTransactions the data manager find by id should return null with no matching VehicleTransaction.
        /// </summary>
        [TestMethod]
        public void VehicleTransactionDataManagerFindByIdShouldReturnNullWithNoMatchingVehicleTransaction()
        {
            var mockObjectSet = new MockObjectSet<VehicleTransaction>();
            mockObjectSet.AddObject(new VehicleTransaction { Id = 1, Milage = 4 });
            mockObjectSet.AddObject(new VehicleTransaction { Id = 2, Milage = 3 });
            mockObjectSet.AddObject(new VehicleTransaction { Id = 3, Milage = 5 });
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.VehicleTransactions).Returns(mockObjectSet);
            var dao = new VehicleTransactionRepository(mockRepository.Object);
            var result = dao.FindById(20);
            Assert.IsNull(result);
            mockRepository.VerifyAll();
        }

        #endregion GetById Tests

        #region Insert Tests

        /// <summary>
        /// Email template dao insert test with empty repository
        /// </summary>
        [TestMethod]
        public void VehicleTransactionDataManagerInsertVehicleTransactionTestWithEmptyRepository()
        {
            var mockObjectSet = new MockObjectSet<VehicleTransaction>();
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.VehicleTransactions).Returns(mockObjectSet);
            mockRepository.Setup(m => m.SaveChanges());
            mockRepository.Setup(m => m.Refresh(RefreshMode.StoreWins, It.IsAny<VehicleTransaction>()));
            var dao = new VehicleTransactionRepository(mockRepository.Object);
            var result = dao.Add(new VehicleTransaction { Id = 1, Milage = 32 });
            Assert.AreEqual(1, mockObjectSet.Count());
            Assert.IsNotNull(result);
            mockRepository.VerifyAll();
        }

        /// <summary>
        /// Email template dao insert test with existing records in repository
        /// </summary>
        [TestMethod]
        public void VehicleTransactionDataManagerInsertVehicleTransactionTestWithNonEmptyRepository()
        {
            var mockObjectSet = new MockObjectSet<VehicleTransaction>();
            mockObjectSet.AddObject(new VehicleTransaction { Id = 1, Milage = 32 });
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.VehicleTransactions).Returns(mockObjectSet);
            mockRepository.Setup(m => m.SaveChanges());
            mockRepository.Setup(m => m.Refresh(RefreshMode.StoreWins, It.IsAny<VehicleTransaction>()));
            var dao = new VehicleTransactionRepository(mockRepository.Object);

            Assert.AreEqual(1, mockObjectSet.Count());

            var result = dao.Add(new VehicleTransaction { Id = 2, Milage = 43 });

            Assert.AreEqual(2, mockObjectSet.Count());
            Assert.IsNotNull(result);
            mockRepository.VerifyAll();
        }

        #endregion Insert Tests

        #region Udpate Tests

        /// <summary>
        /// Email template dao update test with no matching record
        /// </summary>
        [TestMethod]
        public void VehicleTransactionDataManagerUpdateVehicleTransactionTestWithNoMatchingRecord()
        {
            var mockObjectSet = new MockObjectSet<VehicleTransaction>();
            mockObjectSet.AddObject(new VehicleTransaction { Id = 1, Milage = 32 });
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.VehicleTransactions).Returns(mockObjectSet);
            var dao = new VehicleTransactionRepository(mockRepository.Object);
            dao.Update(new VehicleTransaction { TransactionId = 2, Milage = 32 });

            Assert.AreEqual(1, mockObjectSet.Count());
            mockRepository.VerifyAll();
        }

        /// <summary>
        /// Email template dao update test with matching record
        /// </summary>
        [TestMethod]
        public void VehicleTransactionDataManagerUpdateVehicleTransactionTestWithMatchingRecord()
        {
            var mockObjectSet = new MockObjectSet<VehicleTransaction>();
            mockObjectSet.AddObject(new VehicleTransaction { Id = 1, Milage = 32 });
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.VehicleTransactions).Returns(mockObjectSet);
            mockRepository.Setup(m => m.SaveChanges()).Returns(1);

            var dao = new VehicleTransactionRepository(mockRepository.Object);
            dao.Update(new VehicleTransaction { Id = 1, Milage = 32 });

            Assert.AreEqual(1, mockObjectSet.Count());
            mockRepository.VerifyAll();
        }

        #endregion Udpate Tests

        #region Delete Tests

        /// <summary>
        /// Email template dao delete test with no matching record
        /// </summary>
        [TestMethod]
        public void VehicleTransactionDataManagerDeleteVehicleTransactionTestWithNoMatchingRecord()
        {
            var mockObjectSet = new MockObjectSet<VehicleTransaction>();
            mockObjectSet.AddObject(new VehicleTransaction { Id = 1, Milage = 32 });
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.VehicleTransactions).Returns(mockObjectSet);
            var dao = new VehicleTransactionRepository(mockRepository.Object);
            dao.Delete(new VehicleTransaction { Id = 2 });

            Assert.AreEqual(1, mockObjectSet.Count());
        }

        /// <summary>
        /// Email template dao delete test with matching record
        /// </summary>
        [TestMethod]
        public void VehicleTransactionDataManagerDeleteVehicleTransactionTestWithMatchingRecord()
        {
            var mockObjectSet = new MockObjectSet<VehicleTransaction>();
            mockObjectSet.AddObject(new VehicleTransaction { Id = 1, Milage = 323 });
            var mockRepository = new Mock<IDatabase>();
            mockRepository.SetupGet(m => m.VehicleTransactions).Returns(mockObjectSet);
            var dao = new VehicleTransactionRepository(mockRepository.Object);
            dao.Delete(new VehicleTransaction { Id = 1 });

            Assert.AreEqual(0, mockObjectSet.Count());
        }

        #endregion Delete Tests

        #endregion VehicleTransaction Tests
    }
}